package com.vay.de.dang.online.ui.bill;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.vay.de.dang.online.ui.BaseFrag;
import com.vay.de.dang.online.R;
import com.vay.de.dang.online.databinding.FragmentBillBinding;

public class TabBillFragment extends BaseFrag {

    private FragmentBillBinding binding;
    private BillAdapter mAdapter;

    public static TabBillFragment newInstance() {
        Bundle args = new Bundle();
        TabBillFragment fragment = new TabBillFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        binding = FragmentBillBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        View.OnClickListener listener = view -> {
            if (view.getId() == R.id.tab_pending) {
                switchTab(0);
            } else if (view.getId() == R.id.tab_repayment) {
                switchTab(1);
            } else if (view.getId() == R.id.tab_settle) {
                switchTab(2);
            }
        };
        binding.tabPending.setOnClickListener(listener);
        binding.tabRepayment.setOnClickListener(listener);
        binding.tabSettle.setOnClickListener(listener);

        mAdapter = new BillAdapter(getActivity());
        binding.pullView.setRecyclerAdapter(mAdapter);

        switchTab(0);

        return root;
    }

    private void switchTab(int index) {
        if (index == 0) {
            updateTab(binding.tabPending, true);
            updateTab(binding.tabRepayment, false);
            updateTab(binding.tabSettle, false);
            mAdapter.setBillType(1);
        } else if (index == 1) {
            updateTab(binding.tabPending, false);
            updateTab(binding.tabRepayment, true);
            updateTab(binding.tabSettle, false);
            mAdapter.setBillType(2);
        } else {
            updateTab(binding.tabPending, false);
            updateTab(binding.tabRepayment, false);
            updateTab(binding.tabSettle, true);
            mAdapter.setBillType(3);
        }

        binding.pullView.getSwipeRefreshLayout().setRefreshing(true);
        mAdapter.onReload();
    }

    private void updateTab(TextView tv, boolean selected) {
        if (selected) {
            tv.setTextColor(Color.parseColor("#000000"));
            tv.setBackgroundResource(R.drawable.bg_bottom_line);
        } else {
            tv.setTextColor(Color.parseColor("#000000"));
            tv.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mAdapter != null) {
            mAdapter.setRefreshing(true);
            mAdapter.onReload();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}